package com.wande.dataplatform.controller;

import com.wande.common.core.domain.R;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.CheckRuleBo;
import com.wande.dataplatform.domain.bo.CheckTaskBo;
import com.wande.dataplatform.domain.bo.CleanRuleBo;
import com.wande.dataplatform.domain.bo.CleanTaskBo;
import com.wande.dataplatform.domain.dto.CheckResultDTO;
import com.wande.dataplatform.domain.dto.CleanResultDTO;
import com.wande.dataplatform.domain.vo.*;
import com.wande.dataplatform.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 数据质量管理控制器
 *
 * @author wande
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dataplatform/quality")
@RequiredArgsConstructor
public class DataQualityController extends BaseController {

    private final ICheckRuleService checkRuleService;
    private final ICleanRuleService cleanRuleService;
    private final ICheckTaskService checkTaskService;
    private final ICleanTaskService cleanTaskService;
    private final IQualityReportService qualityReportService;

    // ==================== 稽查规则管理 ====================

    /**
     * 查询稽查规则列表
     */
    @GetMapping("/check-rules")
    public TableDataInfo<CheckRuleVo> listCheckRules(CheckRuleBo bo, PageQuery pageQuery) {
        return checkRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取稽查规则详情
     */
    @GetMapping("/check-rules/{id}")
    public R<CheckRuleVo> getCheckRule(@PathVariable Long id) {
        return R.ok(checkRuleService.queryById(id));
    }

    /**
     * 新增稽查规则
     */
    @PostMapping("/check-rules")
    public R<Long> addCheckRule(@Validated @RequestBody CheckRuleBo bo) {
        return R.ok(checkRuleService.insertByBo(bo));
    }

    /**
     * 修改稽查规则
     */
    @PutMapping("/check-rules")
    public R<Void> editCheckRule(@Validated @RequestBody CheckRuleBo bo) {
        checkRuleService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除稽查规则
     */
    @DeleteMapping("/check-rules/{ids}")
    public R<Void> removeCheckRules(@PathVariable List<Long> ids) {
        checkRuleService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 根据维度查询规则
     */
    @GetMapping("/check-rules/dimension/{dimension}")
    public R<List<CheckRuleVo>> getCheckRulesByDimension(@PathVariable String dimension) {
        return R.ok(checkRuleService.queryByDimension(dimension));
    }

    /**
     * 批量导入稽查规则
     */
    @PostMapping("/check-rules/import")
    public R<Void> importCheckRules(@RequestBody List<CheckRuleBo> rules) {
        checkRuleService.importRules(rules);
        return R.ok();
    }

    // ==================== 清洗规则管理 ====================

    /**
     * 查询清洗规则列表
     */
    @GetMapping("/clean-rules")
    public TableDataInfo<CleanRuleVo> listCleanRules(CleanRuleBo bo, PageQuery pageQuery) {
        return cleanRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取清洗规则详情
     */
    @GetMapping("/clean-rules/{id}")
    public R<CleanRuleVo> getCleanRule(@PathVariable Long id) {
        return R.ok(cleanRuleService.queryById(id));
    }

    /**
     * 新增清洗规则
     */
    @PostMapping("/clean-rules")
    public R<Long> addCleanRule(@Validated @RequestBody CleanRuleBo bo) {
        return R.ok(cleanRuleService.insertByBo(bo));
    }

    /**
     * 修改清洗规则
     */
    @PutMapping("/clean-rules")
    public R<Void> editCleanRule(@Validated @RequestBody CleanRuleBo bo) {
        cleanRuleService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除清洗规则
     */
    @DeleteMapping("/clean-rules/{ids}")
    public R<Void> removeCleanRules(@PathVariable List<Long> ids) {
        cleanRuleService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 根据维度查询规则
     */
    @GetMapping("/clean-rules/dimension/{dimension}")
    public R<List<CleanRuleVo>> getCleanRulesByDimension(@PathVariable String dimension) {
        return R.ok(cleanRuleService.queryByDimension(dimension));
    }

    /**
     * 批量导入清洗规则
     */
    @PostMapping("/clean-rules/import")
    public R<Void> importCleanRules(@RequestBody List<CleanRuleBo> rules) {
        cleanRuleService.importRules(rules);
        return R.ok();
    }

    // ==================== 稽查任务管理 ====================

    /**
     * 查询稽查任务列表
     */
    @GetMapping("/check-tasks")
    public TableDataInfo<CheckTaskVo> listCheckTasks(CheckTaskBo bo, PageQuery pageQuery) {
        return checkTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取稽查任务详情
     */
    @GetMapping("/check-tasks/{id}")
    public R<CheckTaskVo> getCheckTask(@PathVariable Long id) {
        return R.ok(checkTaskService.queryById(id));
    }

    /**
     * 新增稽查任务
     */
    @PostMapping("/check-tasks")
    public R<Long> addCheckTask(@Validated @RequestBody CheckTaskBo bo) {
        return R.ok(checkTaskService.insertByBo(bo));
    }

    /**
     * 修改稽查任务
     */
    @PutMapping("/check-tasks")
    public R<Void> editCheckTask(@Validated @RequestBody CheckTaskBo bo) {
        checkTaskService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除稽查任务
     */
    @DeleteMapping("/check-tasks/{ids}")
    public R<Void> removeCheckTasks(@PathVariable List<Long> ids) {
        checkTaskService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 执行稽查任务
     */
    @PostMapping("/check-tasks/{id}/execute")
    public R<CheckResultDTO> executeCheckTask(@PathVariable Long id) {
        return R.ok(checkTaskService.executeCheck(id));
    }

    // ==================== 清洗任务管理 ====================

    /**
     * 查询清洗任务列表
     */
    @GetMapping("/clean-tasks")
    public TableDataInfo<CleanTaskVo> listCleanTasks(CleanTaskBo bo, PageQuery pageQuery) {
        return cleanTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取清洗任务详情
     */
    @GetMapping("/clean-tasks/{id}")
    public R<CleanTaskVo> getCleanTask(@PathVariable Long id) {
        return R.ok(cleanTaskService.queryById(id));
    }

    /**
     * 新增清洗任务
     */
    @PostMapping("/clean-tasks")
    public R<Long> addCleanTask(@Validated @RequestBody CleanTaskBo bo) {
        return R.ok(cleanTaskService.insertByBo(bo));
    }

    /**
     * 修改清洗任务
     */
    @PutMapping("/clean-tasks")
    public R<Void> editCleanTask(@Validated @RequestBody CleanTaskBo bo) {
        cleanTaskService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除清洗任务
     */
    @DeleteMapping("/clean-tasks/{ids}")
    public R<Void> removeCleanTasks(@PathVariable List<Long> ids) {
        cleanTaskService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 执行清洗任务
     */
    @PostMapping("/clean-tasks/{id}/execute")
    public R<CleanResultDTO> executeCleanTask(@PathVariable Long id) {
        return R.ok(cleanTaskService.executeClean(id));
    }

    /**
     * 回滚清洗任务
     */
    @PostMapping("/clean-tasks/{id}/rollback")
    public R<Void> rollbackCleanTask(@PathVariable Long id) {
        cleanTaskService.rollbackClean(id);
        return R.ok();
    }

    // ==================== 质量报告管理 ====================

    /**
     * 查询质量报告列表
     */
    @GetMapping("/reports")
    public TableDataInfo<QualityReportVo> listReports(QualityReportVo vo, PageQuery pageQuery) {
        return qualityReportService.queryPageList(vo, pageQuery);
    }

    /**
     * 获取质量报告详情
     */
    @GetMapping("/reports/{id}")
    public R<QualityReportVo> getReport(@PathVariable Long id) {
        return R.ok(qualityReportService.queryById(id));
    }

    /**
     * 获取任务最新报告
     */
    @GetMapping("/reports/latest/{taskId}")
    public R<QualityReportVo> getLatestReport(@PathVariable Long taskId) {
        return R.ok(qualityReportService.queryLatestByTaskId(taskId));
    }

    /**
     * 获取质量趋势
     */
    @GetMapping("/reports/trend/{tableId}")
    public R<Map<String, Object>> getQualityTrend(
            @PathVariable Long tableId,
            @RequestParam(required = false) LocalDate startDate,
            @RequestParam(required = false) LocalDate endDate) {
        return R.ok(qualityReportService.getQualityTrend(tableId, startDate, endDate));
    }

    /**
     * 获取维度统计
     */
    @GetMapping("/reports/dimension-stats/{tableId}")
    public R<Map<String, Object>> getDimensionStats(@PathVariable Long tableId) {
        return R.ok(qualityReportService.getDimensionStats(tableId));
    }

    /**
     * 删除质量报告
     */
    @DeleteMapping("/reports/{id}")
    public R<Void> removeReport(@PathVariable Long id) {
        qualityReportService.deleteById(id);
        return R.ok();
    }
}
